import {
    createRouter,
    createWebHistory,
    createWebHashHistory
} from 'vue-router'
import main from './modules/main'
import store from '@/store'
import userCenter from './modules/userCenter'

/**
 * 私有路由表
 */
export const privateRoutes = [
    userCenter,
    {
        path: '/home',
        component: () =>
            import(/* webpackChunkName: "login" */ '@/views/page/index')
    }
]

/**
 * 公开路由表
 */
export const publicRoutes = [
    {
        path: '/',
        component: () =>
            import(/* webpackChunkName: "login" */ '@/views/login/index')
    }, {
        path: '/404',
        name: '404',
        component: () =>
            import(/* webpackChunkName: "error-page" */ '@/views/error-page/404')
    },
    {
        path: '/401',
        name: '401',
        component: () =>
            import(/* webpackChunkName: "error-page" */ '@/views/error-page/401')
    }

]

/**
 * 初始化路由表
 */
export function resetRouter() {
    if (
        store.getters.userInfo &&
        store.getters.userInfo.permission &&
        store.getters.userInfo.permission.menus
    ) {
        const menus = store.getters.userInfo.permission.menus
        menus.forEach(menu => {
            router.removeRoute(menu)
        })
    }
}

const router = createRouter({
    history: createWebHashHistory(),
    routes: [...publicRoutes, ...privateRoutes]
})

export default router
